# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=include-what-you-use
pkgver=0.21
# Each release is made for particular LLVM version.
# See https://include-what-you-use.org/downloads/.
_llvmver=17
pkgrel=0
pkgdesc="A tool for use with clang to analyze #includes in C and C++ source files"
url="https://include-what-you-use.org"
arch="all"
license="NCSA"
depends="python3"
# -static packages are needed; include-what-you-use depends on LLVM/clang
# internals and some are exposed only in static libs.
makedepends="
	clang$_llvmver-dev
	clang$_llvmver-extra-tools
	clang$_llvmver-static
	cmake
	llvm$_llvmver-dev
	llvm$_llvmver-gtest
	llvm$_llvmver-static
	samurai
	"
subpackages="$pkgname-dbg $pkgname-doc"
source="$pkgname-$pkgver.tar.gz::https://github.com/include-what-you-use/include-what-you-use/archive/refs/tags/$pkgver.tar.gz"
builddir="$srcdir/$pkgname-$pkgver"

# Tests are broken on other architectures.
if [ "$CARCH" != x86_64 ]; then
	options="!check"
fi

prepare() {
	default_prepare

	sed -i 's|^#!/usr/bin/env python\b|#!/usr/bin/python3|' *.py
}

build() {
	if [ "$CBUILD" != "$CHOST" ]; then
		CMAKE_CROSSOPTS="-DCMAKE_SYSTEM_NAME=Linux -DCMAKE_HOST_SYSTEM_NAME=Linux"
	fi
	cmake -B build -G Ninja \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_INSTALL_LIBDIR=lib \
		-DBUILD_SHARED_LIBS=True \
		-DCMAKE_BUILD_TYPE=RelWithDebInfo \
		-DCMAKE_VERBOSE_MAKEFILE=ON \
		-DCMAKE_PREFIX_PATH=/usr/lib/llvm$_llvmver \
		$CMAKE_CROSSOPTS .
	cmake --build build
}

check() {
	cd build
	CTEST_OUTPUT_ON_FAILURE=1 ctest
}

package() {
	DESTDIR="$pkgdir" cmake --install build

	cd "$pkgdir"
	mv usr/bin/fix_includes.py usr/bin/iwyu-fix-includes
	mv usr/bin/iwyu_tool.py usr/bin/iwyu-tool
}

sha512sums="
d6940fcde5f8212b7d6e1b3b8c9075157f831320279f5b7e57346c292c5b2cc52b53491a2c65b69dfcbd83ae2246c7c71555416e1faad9fd9a0aff9c12ddf1ba  include-what-you-use-0.21.tar.gz
"
